package com.senseauto.rtspplayer;


import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceView;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.misc.ITrackInfo;
import tv.danmaku.ijk.media.widget.media.IjkVideoView;

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    EditText editText;
    IjkVideoView proVideoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText =findViewById(R.id.etUrl);
        proVideoView = findViewById(R.id.surfaceView);

        proVideoView.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int what, int extra) {
                Log.e(TAG, "onError 播放错误");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "播放报错！what="+what+" extra="+extra, Toast.LENGTH_SHORT).show();
                    }
                });
                return true;
            }
        });

        proVideoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {
                Log.e(TAG, "onPrepared onPrepared");

            }
        });

        proVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                Log.e(TAG, "onCompletion");
            }
        });

        //String url = String.format(Constants.ECU_STREAM_URL, Constants.ecuStreamId + 1);
        proVideoView.setVideoPath( "rtsp://10.0.0.34/stream2");
        proVideoView.start();

        findViewById(R.id.btnPlay).setOnClickListener(v->{
            String url = editText.getText().toString().trim();
            if(!TextUtils.isEmpty(url)){
                proVideoView.setVideoPath(url);
                proVideoView.start();
            }
        });
    }

}